package com.fas.hehe.config.controller;

import java.util.HashMap;
import java.util.List;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import com.fas.core.BaseController;
import com.fas.core.constant.R;
import com.fas.core.plugin.jfinal.route.ControllerBind;
import com.fas.core.plugin.tree.jstree.Node;
import com.fas.core.util.datadict.DataDic;
import com.fas.core.util.datadict.DataDicKit;
import com.fas.hehe.config.model.Dictionary;
import com.fas.hehe.config.model.DictionaryData;
import com.fas.hehe.config.service.DictionaryDataService;
import com.fas.hehe.config.service.DictionaryService;

/**
 * 
 * 字典类别管理
 * 
 * TODO 页面表单校验 、状态过滤、
 *
 * @Copyright: Copyright (c) 2016 FAS
 * @anthor yixiaoping<thinker1013@163.com>
 * @since 2016年1月23日 下午12:58:07
 * @version V1.0
 */
@RequiresAuthentication
@ControllerBind(controllerKey = "/config/dictionary", viewPath = "/config/dictionary")
public class DictionaryController extends BaseController {

	public DictionaryService dictionaryService;
	public DictionaryDataService dictionaryDataService;

	// 页面/assets/js/fas/fas.datadic.js 专门使用
	@RequiresPermissions(value = { R.Authority.Dict.QUERY }, logical = Logical.AND)
	public void getDatadicItemsByGroupCode() {
		List<DataDic> list = DataDicKit.me().getDatadicByGroupCode(this.getPara("groupCode"), null);
		this.renderJson(list);
	}

	// 一次性加载字典分类树
	@RequiresPermissions(value = { R.Authority.Dict.QUERY }, logical = Logical.AND)
	public void loadDictionaryTreeData() {
		List<Node> treeNodes = dictionaryService.loadTreeOnceTime();
		HashMap<String, Object> returnData = new HashMap<String, Object>();
		returnData.put("treeNodes", treeNodes);

		this.returnJsonResult(returnData);
	}

	// 加载字典类别下字典数据
	@RequiresPermissions(value = { R.Authority.Dict.QUERY }, logical = Logical.AND)
	public void loadDictionaryDataByDictCode() {

		Dictionary group = dictionaryService.findDictionaryByCode(this.getPara("dictCode"));
		List<DictionaryData> dictDatas = dictionaryDataService.findDictionaryDataByGroupCode(group.getDictCode());

		HashMap<String, Object> returnData = new HashMap<String, Object>();
		returnData.put("group", group);
		returnData.put("dictDatas", dictDatas);

		this.returnJsonResult(returnData);
	}

	// 保存字典类别(for create OR update)
	@RequiresPermissions(value = { R.Authority.Dict.CREATE, R.Authority.Dict.UPDATE }, logical = Logical.OR)
	public void saveDictionary() throws Exception {
		Dictionary dictionary = (Dictionary) this.getModelEntity();
		dictionary = dictionaryService.saveDictionary(dictionary);
		Node node = dictionaryService.buildJstreeNode(dictionary);
		this.returnJsonResult(node);
	}

	// 删除字典类别
	@RequiresPermissions(value = { R.Authority.Dict.DELETE }, logical = Logical.AND)
	public void deleteDictionary() {
		boolean isSuccess = dictionaryService.deleteDictionary(this.getParaToLong("id"));
		this.returnJsonResult(isSuccess);
	}
}
